package com.example.damaimaven.controller;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Component
@Data
@ConfigurationProperties(prefix = "interceptor")
public class LoginInterceptor implements HandlerInterceptor {

    List<String> htmls;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object myinfo = session.getAttribute("myinfo");
        if (myinfo == null) {
            //获取访问的页面
            String requestURI = request.getRequestURI();
            //将页面地址存入会话
            session.setAttribute("requestURI", requestURI);
            response.sendRedirect("/login.html?mustLogin");
            //阻止访问目标资源
            return false;
        }
        //允许访问目标资源
        return true;

    }
}
